package com.codeoss.framework.view.parser;

import org.springframework.stereotype.Service;

import com.codeoss.framework.expression.Expression;
import com.codeoss.framework.expression.ExpressionUtil;
import com.codeoss.framework.view.component.Component;
import com.codeoss.framework.view.component.Container;
import com.codeoss.framework.view.component.Table;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.node.ObjectNode;

@Service
public class TableParser extends AbstractComponentParser {
	protected void setAttributes(IParseContext parseContext, ObjectNode objectNode,Component component) {
		super.setAttributes(parseContext, objectNode, component);
		Table table=(Table)component;
		
		ObjectNode propertyMapNode=(ObjectNode)objectNode.get("propertyMap");
		if(propertyMapNode!=null) {
			JsonNode jsonNode=propertyMapNode.get("dataSource");
			if(jsonNode!=null) {
				String dataSource=propertyMapNode.get("dataSource").asText();
				table.setDataSource(dataSource);
			}
			jsonNode=propertyMapNode.get("editable");
			if(jsonNode!=null) {
				boolean editable=propertyMapNode.get("editable").asBoolean();
				table.setEditable(editable);
			}
			
			jsonNode=propertyMapNode.get("pageable");
			if(jsonNode!=null) {
				boolean pageable=propertyMapNode.get("pageable").asBoolean();
				table.setPageable(pageable);
			}
			
			jsonNode=propertyMapNode.get("onPagination");
			if(jsonNode!=null) {
				String onPaginationExpression=jsonNode.asText();
				String expressionType=Expression.EXPRESSION_TYPE_JAVASCRIPT;
				if(onPaginationExpression.startsWith("@")) {
					expressionType=Expression.EXPRESSION_TYPE_SPRING;
				}
				Expression expression=ExpressionUtil.convertToExpression(onPaginationExpression,expressionType);
				table.setOnPagination(expression);
			}
			
			jsonNode=propertyMapNode.get("multiSelect");
			if(jsonNode!=null) {
				boolean multiSelect=propertyMapNode.get("multiSelect").asBoolean();
				table.setMultiSelect(multiSelect);
			}
			
			jsonNode=propertyMapNode.get("halfEditable");
			if(jsonNode!=null) {
				boolean halfEditable=propertyMapNode.get("halfEditable").asBoolean();
				table.setHalfEditable(halfEditable);
			}
			
			jsonNode=propertyMapNode.get("halfEditable");
			if(jsonNode!=null) {
				boolean halfEditable=propertyMapNode.get("halfEditable").asBoolean();
				table.setHalfEditable(halfEditable);
			}
			
			jsonNode=propertyMapNode.get("formViewPageCode");
			if(jsonNode!=null) {
				String formViewPageCode=propertyMapNode.get("formViewPageCode").asText();
				table.setFormViewPageCode(formViewPageCode);
			}
			jsonNode=propertyMapNode.get("onSort");
			if(jsonNode!=null) {
				String onSortExpression=jsonNode.asText();
				String expressionType=Expression.EXPRESSION_TYPE_JAVASCRIPT;
				if(onSortExpression.startsWith("@")) {
					expressionType=Expression.EXPRESSION_TYPE_SPRING;
				}
				Expression expression=ExpressionUtil.convertToExpression(onSortExpression,expressionType);
				table.setOnSort(expression);
			}
		}
	}
	
	protected void parseChildren(IParseContext parseContext, ObjectNode objectNode,Container container) {
		Table table=(Table)container;
		parseContext.setAttribute("dataSource", table.getDataSource());
		super.parseChildren(parseContext, objectNode, container);
		parseContext.removeAttribute("dataSource");
	}
	
	@Override
	protected Component createComponent(IParseContext parseContext, ObjectNode objectNode) {
		// TODO Auto-generated method stub
		return new Table();
	}

}
